package com.st.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.st.dao.IEmpDao;
import com.st.entity.Emp;
import com.st.util.DBUtil;

public class EmpDaoImpl implements IEmpDao {

	private DBUtil dbUtil = new DBUtil();
	
	// 搜索
	@Override
	public List select(Emp emp) {
		String sql = "select emp.*,dept.dname from emp,dept where emp.did=dept.did ";
		List params = new ArrayList<>();

		if (emp.getEname() != null && emp.getEname().length() > 0) {
			sql += " and emp.ename like ? ";// %%
			params.add("%"+emp.getEname()+"%");
		}
		if (emp.getEphone() != null && emp.getEphone().length() > 0) {
			sql += " and emp.ephone like ?"; // %%
			params.add("%"+emp.getEphone()+"%");
		}
//		params.add("%"+emp.getEname()+"%");
//		params.add("%"+emp.getEphone()+"%");
		if (emp.getDid() != null) {
			sql += " and emp.did = ?";// did=null
			params.add(emp.getDid());
		}

		return dbUtil.query(sql, params.toArray());
	}

	@Override
	public int delete(int eid) {
		String sql = "delete from emp where eid=?";
		Object[] params = {eid};
		return dbUtil.update(sql, params);
	}

	@Override
	public int insert(Emp emp) {
		String sql = "insert into emp (ename,ephone,ebirthday,ephoto,did) value(?,?,?,?,?)";
		Object[] params = {emp.getEname(),emp.getEphone(),emp.getEbirthday(),emp.getEphoto(),emp.getDid()};
		return dbUtil.update(sql, params);
	}

	@Override
	public int updatePhoto(Integer eid, String ephoto) {
		String sql = "update emp set ephoto=? where eid=?";
		Object[] params = {ephoto,eid};
		return dbUtil.update(sql, params);
	}

}
